package com.corner.utils.view;

import android.app.Activity;
import android.content.Context;
import android.view.View;

import java.lang.reflect.Field;

/**
 * findviewById���乤��
 *
 * @author Administrator
 */
public class InjectViewUtils {
    public static void injectView(Activity activity) {
        Class<? extends Activity> actClass = activity.getClass();
        Field field[] = actClass.getDeclaredFields();
        for (Field item : field) {
            if (item.isAnnotationPresent(FindView.class)) {
                try {
                    int id = item.getAnnotation(FindView.class)
                            .value();
                    item.setAccessible(true);
                    item.set(activity, activity.findViewById(id));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
/*            if (item.isAnnotationPresent(CustomFont.class)) {
                try {
                    String fontName = ((CustomFont) item.getAnnotation(CustomFont.class))
                            .value();
                    item.setAccessible(true);
                    TypeFaceUtil.setTypeFace(activity, ((TextView) item.get(activity)), fontName);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }*/
        }
    }

    public static void injectView(Object object, View root, Context context) {
        Class clazz = object.getClass();
        Field field[] = clazz.getDeclaredFields();
        for (Field item : field) {
            if (item.isAnnotationPresent(FindView.class)) {
                try {
                    int id = item.getAnnotation(FindView.class)
                            .value();
                    item.setAccessible(true);
                    item.set(object, root.findViewById(id));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
/*            if (item.isAnnotationPresent(CustomFont.class)) {
                try {
                    String fontName = ((CustomFont) item.getAnnotation(CustomFont.class))
                            .value();
                    item.setAccessible(true);
                    TypeFaceUtil.setTypeFace(context, ((TextView) item.get(object)), fontName);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }*/
        }
    }

    public static void injectView(Object object, Activity root) {
        Class clazz = object.getClass();
        Field field[] = clazz.getDeclaredFields();
        for (Field item : field) {
            if (item.isAnnotationPresent(FindView.class)) {
                try {
                    int id = item.getAnnotation(FindView.class)
                            .value();
                    item.setAccessible(true);
                    item.set(object, root.findViewById(id));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
/*            if (item.isAnnotationPresent(CustomFont.class)) {
                try {
                    String fontName = ((CustomFont) item.getAnnotation(CustomFont.class))
                            .value();
                    item.setAccessible(true);
                    TypeFaceUtil.setTypeFace(root, ((TextView) item.get(object)), fontName);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }*/
        }
    }

}
